import { LLMNode } from "./llmNode";
import { Annotation, } from "@langchain/langgraph";
import { baseInputVars } from "./baseInputVars";
import { GenID } from "../../utils";

export class StartNode extends LLMNode {
    name = "StartNode";
    inputVars = Annotation.Root({
        ...baseInputVars.spec,
    })
    outputVars = Annotation.Root({
        StartNodeResult: Annotation<string>,
    });
    isGenTitle = true;
    async Run() {
        const convItem = await this.storeMsgSvr.GetConversationById(this.inputVars.State.convID);
        if (!convItem) {
            if (this.isGenTitle) {
                this.userMessages = [
                    `
                根据用户输入，总结一个对话标题，10个字以内
                ## 用户输入： ${this.inputVars.State.userInput}

                ## 输出：
                仅仅输出标题
                `
                ];
                await super.Run();
            }

            await this.storeMsgSvr.CreateConv({
                _id: this.inputVars.State.convID,
                userID: this.inputVars.State.userID,
                title: this.outputVars.State.StartNodeResult,
                createdAt: new Date(),
                updatedAt: new Date(),
                state: 0, // 0: normal, 1: deleted
                workflowID: this.workflowID,
            });
        }

        await this.storeMsgSvr.AddToChatHistory({
            _id: GenID(),
            userID: this.inputVars.State.userID,
            convID: this.inputVars.State.convID,
            content: this.inputVars.State.userInput,
            contentType: "text",
            type: "user",
            createdAt: new Date()
        });

        return this.outputVars.State;
    }
}